package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysPositionsApplyMapper;
import com.ruoyi.system.domain.SysPositionsApply;
import com.ruoyi.system.service.ISysPositionsApplyService;

/**
 * 报名管理信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-17
 */
@Service
public class SysPositionsApplyServiceImpl implements ISysPositionsApplyService 
{
    @Autowired
    private SysPositionsApplyMapper sysPositionsApplyMapper;

    /**
     * 查询报名管理信息
     * 
     * @param applyId 报名管理信息主键
     * @return 报名管理信息
     */
    @Override
    public SysPositionsApply selectSysPositionsApplyByApplyId(Long applyId)
    {
        return sysPositionsApplyMapper.selectSysPositionsApplyByApplyId(applyId);
    }

    /**
     * 查询个人报名管理信息
     *
     * @param sysPositionsApply 报名个人管理信息主键
     * @return 报名管理信息
     */

    public List<SysPositionsApply> selectSysPositionsApplyByidcardAndBatchNumber(SysPositionsApply sysPositionsApply){
        System.out.println("查询到个人信息：" + sysPositionsApplyMapper.selectSysPositionsApplyByidcardAndBatchNumber(sysPositionsApply));
        return sysPositionsApplyMapper.selectSysPositionsApplyByidcardAndBatchNumber(sysPositionsApply);
    };





    /**
     * 查询报名管理信息列表
     * 
     * @param sysPositionsApply 报名管理信息
     * @return 报名管理信息
     */
    @Override
    public List<SysPositionsApply> selectSysPositionsApplyList(SysPositionsApply sysPositionsApply)
    {
        return sysPositionsApplyMapper.selectSysPositionsApplyList(sysPositionsApply);
    }

    /**
     * 新增报名管理信息
     * 
     * @param sysPositionsApply 报名管理信息
     * @return 结果
     */
    @Override
    public int insertSysPositionsApply(SysPositionsApply sysPositionsApply)
    {
        sysPositionsApply.setCreateTime(DateUtils.getNowDate());
        return sysPositionsApplyMapper.insertSysPositionsApply(sysPositionsApply);
    }

    /**
     * 修改报名管理信息
     * 
     * @param sysPositionsApply 报名管理信息
     * @return 结果
     */
    @Override
    public int updateSysPositionsApply(SysPositionsApply sysPositionsApply)
    {
        sysPositionsApply.setUpdateTime(DateUtils.getNowDate());
        return sysPositionsApplyMapper.updateSysPositionsApply(sysPositionsApply);
    }

    /**
     * 批量删除报名管理信息
     * 
     * @param applyIds 需要删除的报名管理信息主键
     * @return 结果
     */
    @Override
    public int deleteSysPositionsApplyByApplyIds(Long[] applyIds)
    {
        return sysPositionsApplyMapper.deleteSysPositionsApplyByApplyIds(applyIds);
    }

    /**
     * 删除报名管理信息信息
     * 
     * @param applyId 报名管理信息主键
     * @return 结果
     */
    @Override
    public AjaxResult deleteSysPositionsApplyByApplyId(Long applyId)
    {
        String processID=sysPositionsApplyMapper.selectSysPositionsApplyByApplyId(applyId).getProcessId();
        sysPositionsApplyMapper.deleteSysPositionsApplyByApplyId(applyId);
        return AjaxResult.success("流程启动成功", processID);
    }

    public SysPositionsApply getApplyByProcessId(String processId) {
        return sysPositionsApplyMapper.selectSysPositionsApplyByProcessId(processId);
    }


    public int deleteSysPositionsApplyByProcessId(String processId) {
        return sysPositionsApplyMapper.deleteSysPositionsApplyByProcessId(processId);
    }

    /**
     * 查询一周报名信息
     *
     *
     * @return 结果
     */
    @Override
    public List<SysPositionsApply> selectWeeklyApplyData(SysPositionsApply sysPositionsApply) {
        return sysPositionsApplyMapper.selectWeeklyApplyData(sysPositionsApply);
    }


    /**
     * 获取报名权限
     * @return
     */

    // 定义返回码常量
    private static final int CODE_USER_NOT_LOGIN = 1;
    private static final int CODE_NO_APPLY_RECORD = 2;
    private static final int CODE_APPLY_COUNT_EXCEEDED = 3;
    private static final int CODE_PENDING_APPLY_EXIST = 4;
    private static final int CODE_APPROVED_APPLY_EXIST = 5;
    private static final int CODE_CAN_APPLY = 0;

    @Override
    public int checkApplyPermission() {
        // 获取当前登录用户信息
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        if (currentUser == null || currentUser.getUserId() == null) {
            return CODE_USER_NOT_LOGIN; // 用户未登录
        }

        // 查询当前用户的所有报名记录
        SysPositionsApply apply = new SysPositionsApply();
        apply.setUserId(currentUser.getUserId()); // 设置用户ID查询条件

        List<SysPositionsApply> applyList = sysPositionsApplyMapper.selectSysPositionsApplyList(apply);

        //第二版需求，用户可以填写多个职位，不超过5个
        if (applyList == null || applyList.isEmpty()) {
            return CODE_CAN_APPLY; // 用户没有报名记录
        }
        if (applyList.size() >= 5) {
            return CODE_APPLY_COUNT_EXCEEDED; // 报名记录超过5条
        }
        return CODE_CAN_APPLY; // 可以申请

        //第一版需求
//        if (applyList == null || applyList.isEmpty()) {
//            return CODE_CAN_APPLY; // 用户没有报名记录
//        }
//        if (applyList.size() >= 3) {
//            return CODE_APPLY_COUNT_EXCEEDED; // 报名记录超过3条
//        }
//
//
//        boolean hasPendingApply = false;
//        boolean hasApprovedApply = false;
//
//        for (SysPositionsApply a : applyList) {
//            Long status = a.getStatusApply();
//            if (status == null) {
//                continue; // 忽略 null 状态
//            }
//            if (status == 0L) {
//                hasPendingApply = true;
//            } else if (status == 1L) {
//                hasApprovedApply = true;
//            }
//            // 可提前终止循环
//            if (hasPendingApply && hasApprovedApply) {
//                break;
//            }
//        }
//
//        if (hasPendingApply) {
//            return CODE_PENDING_APPLY_EXIST; // 存在未处理的申请
//        }
//
//        if (hasApprovedApply) {
//            return CODE_APPROVED_APPLY_EXIST; // 存在已通过的申请
//        }
//
//        return CODE_CAN_APPLY; // 可以申请
    }

    @Override
    public List<com.ruoyi.system.domain.vo.SysApplyExportVO> selectSysPositionsApplyExportList(SysPositionsApply sysPositionsApply) {
        return sysPositionsApplyMapper.selectSysPositionsApplyExportList(sysPositionsApply);
    }

}
